﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory

    ''' <summary>
    ''' A class that caches the factions available on the Armory
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Factions
        Inherits WowArmoryData

        Private fsPage As New FactionSchema.page

        ''' <summary>
        ''' The URL of the faction data
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL() As String
            Get
                Return "http://www.wowarmory.com/data/factionStrings.xml"
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin() As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Returns a list of available factions
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Factions() As List(Of Faction)
            Get
                If fsPage Is Nothing Then
                    Return New List(Of Faction)
                End If
                Return (From a In fsPage.factions(0).faction Select New Faction(a.id, a.name, CType(a.release, ReleaseType))).ToList()
            End Get
        End Property

        ''' <summary>
        ''' Constructor that retrieves the fuction data from either the armory or the local cache
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            If Cache.Cache("LibWowArmory.Factions") Is Nothing Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(fsPage.GetType())

                Dim srXML As New StringReader(MyBase.strData)

                fsPage = CType(xsSerializer.Deserialize(srXML), FactionSchema.page)

                Cache.Cache.Insert("LibWowArmory.Factions", fsPage, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.NotRemovable, Nothing)
            Else
                fsPage = CType(Cache.Cache("LibWowArmory.Factions"), FactionSchema.page)
            End If
        End Sub

        ''' <summary>
        ''' A class that encapsulates a single faction returned from the Armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Class Faction

            Private intID As Integer
            Private strName As String
            Private rRelease As ReleaseType

            ''' <summary>
            ''' The faction's identification number
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property ID() As Integer
                Get
                    Return intID
                End Get
                Set(ByVal value As Integer)
                    intID = value
                End Set
            End Property

            ''' <summary>
            ''' The name of the faction
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Name() As String
                Get
                    Return strName
                End Get
                Set(ByVal value As String)
                    strName = value
                End Set
            End Property

            ''' <summary>
            ''' The release of World of Warcraft the faction was introduced in
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Release() As ReleaseType
                Get
                    Return rRelease
                End Get
                Set(ByVal value As ReleaseType)
                    rRelease = value
                End Set
            End Property

            ''' <summary>
            ''' Constructor to create a new faction object
            ''' </summary>
            ''' <param name="intID"></param>
            ''' <param name="strName"></param>
            ''' <param name="rRelease"></param>
            ''' <remarks></remarks>
            Friend Sub New(ByVal intID As Integer, ByVal strName As String, ByVal rRelease As ReleaseType)
                Me.intID = intID
                Me.strName = strName
                Me.rRelease = rRelease
            End Sub

        End Class

    End Class

End Namespace